#!/usr/bin/env python
#-*-encoding:utf-8-*-
'''
Created on 2015年7月10日

@author: chenyongbing
'''
import sys,os,datetime
#current_dir = os.path.dirname(__file__)
current_dir = os.path.split(os.path.realpath(__file__))[0]

'''
    按照 驻店时长 统计到访次数
'''


sys.path.append(os.path.join(current_dir,'../../'))

from base.time_method import myTimeMethod

tab = 'newhivehistoryrole'
st = '2015-07-08'
et = '2015-07-14'
dgroup = 32015231

durTypes = [{'name':'0-10秒','value':[0,10]},
           {'name':'10-30秒','value':[11,30]},
           {'name':'31-60秒','value':[31,60]},
           {'name':'1-2分','value':[61,120]},
           {'name':'2-5分','value':[121,300]},
           {'name':'5-10分','value':[301,600]},
           {'name':'10-20分','value':[601,1200]},
           {'name':'20分以上','value':[1201,24*60*60]},
           ]
durTypes = None          
def get_datas_from_role_file(rolePath='/Udisk/new_role',day=None,group=''):
    rolefile = rolePath +'/' + day +'/part-00000'
    datas = []
    for line in open(rolefile).read().split('\n'):
        #2015-07-14,32015106,98:03:d8:78:05:7f,2015-07-14 18:24:58,2015-07-14 18:30:43,345000,1,Apple
        lineList = line.split(',')
        if len(lineList)!=8:continue
        grp = lineList[1]
        if str(group) != str(grp):continue
        mac = lineList[2]
        dur = lineList[5]
        datas.append([mac,int(dur)])
    return datas
today = datetime.date.today().strftime('%Y-%m-%d')
fr = open('../report/visit_freq_by_dur.%s.txt'%today,'w')
for day in myTimeMethod.get_slist_between_st_et(st, et):     

    role = None
    
#     query = 'select mac,dur from %s where day= "%s"  and grp="%s" and role!=2'%(tab,day,dgroup)
#     rets = myHiveClient.SelectAll(query)
#     
    
    rets = get_datas_from_role_file(day=day,group=dgroup)
    datas = {}
    if durTypes!=None:
        for mac,dur in rets:
    
            for durType in durTypes:
                name = durType['name']
                min = durType['value'][0]
                max = durType['value'][1]
                if dur >= min *1000 and dur <= max*1000:
                    if name  not in datas.keys():
                        datas[name] = 0
                    datas[name]+=1
    
                
                
        for durType     in durTypes:
            
            key = durType['name']
            if key in datas.keys():
                count = datas[key]
            else:
                count = 0
            print '\t'.join([day,key,str(count)])
            print ''
            print  ''
    else:
        for mac,dur in rets:
            if int(dur)>20*60*1000:
                num = 999
            else:
                num = (int(dur)/1000 + 4)/5
            datas[num]+=1
        keys =  datas.keys()
        
        keys.sort()
        
        
        for key in keys:
            if key == 999:
                name = '其他'
            else:
                name = '%s-%s秒'%((key-1)*5,key*5)
            count = str(datas[key])
            total = sum(datas.values())
            zb = str((round(float(count)/float(total),4))*100)+'%'
            print '\t'.join([day,name,count,zb])
            fr.write('\t'.join([day,name,count,zb])+'\n')
        
    print ''
    fr.write('\n')
fr.close()